package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.MachineInfoLogic;
import cn.topevery.garden.entity.dto.MachineInfoDTO;
import cn.topevery.garden.entity.query.MachineInfoQuery;
import cn.topevery.garden.entity.vo.MachineInfoVO;
import cn.topevery.garden.entity.po.MachineInfo;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.dozermapper.core.Mapper;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;

/**
 * 机械信息
 */
@Api(tags = "机械信息")
@RestController
@RequestMapping("/machineInfo")
public class MachineInfoController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    MachineInfoLogic machineInfoLogic;


    /**
     * 获取机械信息分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取机械信息页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<MachineInfoVO>> listPage(@RequestBody MachineInfoQuery query) {
        return JsonResult.ok(machineInfoLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 机械信息信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody MachineInfoDTO dto) {
        if(StringUtils.isBlank(dto.getEntityId())){
            dto.setEntityId(UUID.randomUUID().toString());
        }
        machineInfoLogic.saveOrUpdate(getUser().id.toString(),mapper.map(dto, MachineInfo.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<MachineInfoVO> getInfoById(@RequestParam Integer id) {
        MachineInfo entity = machineInfoLogic.getEntity(id);
        MachineInfoVO vo= mapper.map(entity, MachineInfoVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        machineInfoLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出机械信息信息")
    @GetMapping("/export")
    public void export(MachineInfoQuery query, HttpServletResponse response) {
        List<MachineInfoVO> list = machineInfoLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "机械信息", "sheet1", MachineInfoVO.class, "机械信息", response);
    }

}
